package cn.itcast.protocol.testCodeC;

import cn.itcast.message.LoginRequestMessage;
import cn.itcast.message.Message;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.ByteToMessageCodec;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

public class TestMessageCodec{

    public static void main(String[] args) throws Exception {

        LoggingHandler LOGGING_DECODER = new LoggingHandler();
        EmbeddedChannel embeddedChannel = new EmbeddedChannel(LOGGING_DECODER, new LengthFieldBasedFrameDecoder(1024, 12, 4, 0, 0), new MessageCodec());

        LoginRequestMessage message = new LoginRequestMessage("zhagnsan", "123");
//        embeddedChannel.writeOneOutbound(message);

        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        new MessageCodec().encode(null, message, buf);
//        embeddedChannel.writeInbound(buf);

        ByteBuf s1 = buf.slice(0, 100);
        s1.retain();
        ByteBuf s2 = buf.slice(100, buf.readableBytes() - 100);
        s2.retain();
        embeddedChannel.writeInbound(s1);

        embeddedChannel.writeInbound(s2);
    }
}

